package com.rpc.proxy.impl.cglib;

import com.rpc.proxy.impl.InvokerProxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description: cglib代理实现类
 * @date 2017-12-28-下午5:37
 */

public class CglibInvokerInvocationHandler implements InvokerProxy {

    Enhancer enhancer = new Enhancer();

    @Override
    public Object bind(Class objectClass) {
        enhancer.setSuperclass(objectClass);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args,
                                    MethodProxy proxy) throws Throwable {
                System.out.println("I'm proxy!");
                Object res = method.invoke(objectClass, args);
                return res;
            }
        });
        return enhancer.create();
    }
}
